package ru.ssau;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import lab.bmp.LookupHelper;
import lab.bmp.artist.Artist;
import lab.bmp.artist.ArtistHome;
import lab.bmp.artist.ArtistPK;
import lab.bmp.genre.Genre;
import lab.bmp.genre.GenreHome;
import lab.bmp.genre.GenrePK;
import lab.bmp.studio.Studio;
import lab.bmp.studio.StudioHome;
import lab.bmp.studio.StudioPK;
import lab.bmp.tag.TagHome;
import lab.bmp.track.Track;
import lab.bmp.track.TrackHome;
import lab.bmp.track.TrackPK;

/**
 *
 * @author Smike
 */
public class CollectionDAOBean implements SessionBean {

    private SessionContext context;
    private Connection connection;
    private TagHome tagHome;
    private ArtistHome artistHome;
    private GenreHome genreHome;
    private StudioHome studioHome;
    private TrackHome trackHome;

    public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
        context = ctx;
        try {
            LookupHelper helper = new LookupHelper();
            tagHome = helper.lookup(TagHome.class);
            artistHome = helper.lookup(ArtistHome.class);
            genreHome = helper.lookup(GenreHome.class);
            studioHome = helper.lookup(StudioHome.class);
            trackHome = helper.lookup(TrackHome.class);
        } catch (NamingException ex) {
            Logger.getLogger(CollectionDAOBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void ejbRemove() throws EJBException, RemoteException {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(CollectionDAOBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public String getDemo() {
        return "Hello World!";
    }

    public Collection findTags() {
        try {
            return tagHome.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Collection findArtists() {
        try {
            return artistHome.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Artist findArtist(ArtistPK id) {
        try {
            return artistHome.findByPrimaryKey(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Collection findGenres() {
        try {
            return genreHome.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Genre findGenre(GenrePK id) {
        try {
            return genreHome.findByPrimaryKey(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Collection findStudios() {
        try {
            return studioHome.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Studio findStudio(StudioPK id) {
        try {
            return studioHome.findByPrimaryKey(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Collection findTracks() {
        try {
            return trackHome.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Track findTrack(TrackPK id) {
        try {
            return trackHome.findByPrimaryKey(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void insertStudio(lab.domain.SoundRecordingStudio studio) {
        try {
            DatabaseHelper.insertStudio(getConnection(), studio);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void insertTrack(lab.domain.Track track) {
        try {
            DatabaseHelper.insertTrack(getConnection(), track);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void insertGenre(lab.domain.Genre genre) {
        try {
            DatabaseHelper.insertGenre(getConnection(), genre);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void insertArtist(lab.domain.Artist artist) {
        try {
            DatabaseHelper.insertArtist(getConnection(), artist);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void deleteStudio(StudioPK studio) {
        try {
            studioHome.remove(studio);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void deleteTrack(TrackPK track) {
        try {
            trackHome.remove(track);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void deleteGenre(GenrePK genre) {
        try {
            genreHome.remove(genre);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void deleteArtist(ArtistPK artist) {
        try {
            artistHome.remove(artist);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    
    private Connection getConnection() {
        try {
            if (connection == null || connection.isClosed()) {
                try {
                    DataSource datasource = (DataSource) new InitialContext().lookup("oracleDS");
                    connection = datasource.getConnection();
                } catch (NamingException ex) {
                    throw new EJBException(ex);
                }
            }
            return connection;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}
